import Character from "./Character";
import Desk from "./Desk";
import Fraction from "./Fraction";
import winPanel from "./winPanel";

const {ccclass, property} = cc._decorator;

@ccclass
export default class GameManager extends cc.Component {

    @property(cc.Prefab)
    characterPrefab:cc.Prefab=null;
        
    @property(Fraction)
    fraction:Fraction=null;
     
    @property(winPanel)
    winPanel:winPanel=null;
    

    desk:Desk;   
    characters:{[uid:number]:Character}={}
    
    //底牌 
    bottomCards:QM.Card[] = []

    onLoad () {    
        this.desk = this.node.getChildByName("Desk").getComponent(Desk);         
        
        
        // var ch:QM.Character = {uid:60001,username:"天下",point:QM.QPoint.Bottom,fd:1};
        // QM.User = ch;
        // var ch1:QM.Character = {uid:6002,username:"无双",point:QM.QPoint.Left,fd:2};
        // var ch2:QM.Character = {uid:6003,username:"英豪",point:QM.QPoint.Right,fd:3};
        // QM.Room.Instance.addCharacter(ch);
        // QM.Room.Instance.addCharacter(ch1);
        // QM.Room.Instance.addCharacter(ch2);
        // QM.Room.Instance.landLord = 6002;





        

        if(!QM.User){
            cc.director.loadScene("hall");
            return ;
        }
      
        this.node.on("GameReset",()=>{        
            this.GameReset();
        },this);
      
        QM.Socket.Instance.on(this.message,this);
        this.createCharacter();

      
      
      
      
        // this.scheduleOnce(()=>{
        //     this.showGameOverPanel({winUid:6002});
        // },3)


    }



    message(ev:QM.QEvent){
       // cc.log(ev);
        var cmd = ev.cmd;
        var data = ev.data;
        if(cmd && this[cmd]){
            this[cmd](data);
        }        
    }

  

    
    /** 创建角色 */
    createCharacter(){        
        var room = QM.Room.Instance;
        var chs = room.getCharacters();
        chs.forEach(ch=>{            
            if(!this.characters[ch.uid]){
                var node = cc.instantiate(this.characterPrefab);           
                node.parent = this.node;
                var C = node.getComponent(Character);            
                C.character = ch;
                this.characters[ch.uid] = C;                         
            }
        })       
    }

    /** 发牌 */
    Licensing(obj:{uid:number,cards:QM.Card[]|number,bottom?:QM.Card[]}[]) {                 
        for(var i=0;i<obj.length;i++){
            if(obj[i].bottom){
                this.bottomCards = obj[i].bottom;
                this.desk.receiveBottomCards(this.bottomCards);
                break;
            }       
        }
        for(let k in this.characters){            
            this.characters[k].receiveCards(obj);
        }
    }

    /** 发底牌 */
    LicensingBottom(obj:{uid:number}){  
        //关闭叫分面板
        this.hideFractionPanel();

        //设置房间身份
        if(!QM.Room.Instance.landLord){
            QM.Room.Instance.landLord = obj.uid;          
        }
        for(let k in this.characters){
            this.characters[k].showCamp();
        }


        this.characters[obj.uid].receiveBottomCards({uid:obj.uid,cards:this.bottomCards});
    }
    /** 某玩家退出房间 */
    exitRoom(obj:{user:QM.Character}){
        
        
        QM.Room.Instance.removeCharacter(obj.user);
        this.characters[obj.user.uid].node.parent = null;
        delete this.characters[obj.user.uid];
        this.GameReset();
        

    }
    /** 某玩家进入房间 */
    intoRoom(obj:{rid:number,user:QM.Character}){
        if(obj.user.uid==QM.User.uid) return false;
        QM.Room.Instance.addCharacter(obj.user);
        this.createCharacter();
    }
    
    /** 显示叫地主面板 */
    showFractionPanel(obj:{uid:number,max:number}){
        var user = QM.Room.Instance.getCharacterForUid(obj.uid) ;
        var max = obj.max;
        this.fraction.show(user,max);
    }

    /** 隐藏叫分面板 */
    hideFractionPanel(){
        this.fraction.node.active = false;
    }


    /** 显示游戏结束面板 */
    showGameOverPanel(obj:{winUid:number}){
        cc.log(obj);
        this.winPanel.show(obj);
    }

    /** 游戏重置 */
    GameReset(){
        this.winPanel.hide();
        //将房间地主清除
        QM.Room.Instance.landLord = 0;
        this.desk.reset();
        //清除角色的卡牌，重置状态为等待
        for(let k in this.characters){
            this.characters[k].showReadyButton();
        }
    }

    onDestroy(){
        QM.Socket.Instance.off(this.message,this); 
    }
    
 

}
